package top.fangw.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.seata.common.util.CollectionUtils;
import io.seata.common.util.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import top.fangw.admin.mapper.SysMenuMapper;
import top.fangw.admin.pojo.dto.SysOrgQueryDto;
import top.fangw.admin.pojo.entity.SysMenu;
import top.fangw.admin.pojo.entity.SysOrgInfo;
import top.fangw.admin.mapper.SysOrgInfoMapper;
import top.fangw.admin.service.ISysOrgInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.fangw.admin.service.ISysUserJoinOrgService;
import top.fangw.common.api.vo.InitOrgVo;
import top.fangw.common.core.exception.BizException;
import top.fangw.common.api.vo.InitUserJoinOrgVo;
import top.fangw.common.mybatis.annotation.DyDataSource;
import top.fangw.common.mybatis.service.BaseTreeService;

import java.util.List;

/**
 * <p>
 * 用途：存储机构信息 服务实现类
 * </p>
 *
 * @author fangw
 * @since 2024-03-02
 */
@Service
@DS("base_info")
@RequiredArgsConstructor
public class SysOrgInfoServiceImpl extends ServiceImpl<SysOrgInfoMapper, SysOrgInfo> implements ISysOrgInfoService {

    private final SysOrgInfoMapper sysOrgInfoMapper;

    private final ISysUserJoinOrgService sysUserJoinOrgService;

    @Override
    @DyDataSource(tenantId = "#initOrgVo.tenantId", autoTenantId = false)
    public InitOrgVo initTenantOrg(InitOrgVo initOrgVo) {
        // 初始化机构信息
        SysOrgInfo sysOrgInfo = BeanUtil.copyProperties(initOrgVo, SysOrgInfo.class);
        BaseTreeService<SysOrgInfoMapper, SysOrgInfo> baseTreeService = new BaseTreeService<>(sysOrgInfoMapper);
        baseTreeService.saveOrUpdateTree(sysOrgInfo);
        //初始化用户与机构的关联关系
        InitUserJoinOrgVo initUserJoinOrg = new InitUserJoinOrgVo();
        initUserJoinOrg.setTenantId(sysOrgInfo.getTenantId());
        initUserJoinOrg.setOrgId(sysOrgInfo.getId());
        initUserJoinOrg.setUserId(sysOrgInfo.getMaster());
        initUserJoinOrg.setType(sysOrgInfo.getType());
        sysUserJoinOrgService.initUserJoinOrg(initUserJoinOrg);
        return BeanUtil.copyProperties(sysOrgInfo, InitOrgVo.class);
    }

    @Override
    @DyDataSource
    public void add(SysOrgInfo sysOrgInfo) {
        BaseTreeService<SysOrgInfoMapper, SysOrgInfo> treeService = new BaseTreeService<>(sysOrgInfoMapper);
        treeService.saveOrUpdateTree(sysOrgInfo);
    }

    @Override
    @DyDataSource
    public IPage<SysOrgInfo> pageList(SysOrgQueryDto sysOrgQueryDto) {
        BaseTreeService<SysOrgInfoMapper, SysOrgInfo> treeService = new BaseTreeService<>(sysOrgInfoMapper);
        LambdaQueryWrapper<SysOrgInfo> queryWrapper = new LambdaQueryWrapper<SysOrgInfo>().orderByAsc(SysOrgInfo::getSort);
        if (StrUtil.isNotBlank(sysOrgQueryDto.getName())) {
            queryWrapper.like(SysOrgInfo::getName, sysOrgQueryDto.getName());
        }
        return treeService.pageList(queryWrapper, sysOrgQueryDto.getCurrent(), sysOrgQueryDto.getSize());
    }

    @Override
    public List<SysOrgInfo> queryList() {
        BaseTreeService<SysOrgInfoMapper, SysOrgInfo> treeService = new BaseTreeService<>(sysOrgInfoMapper);
        LambdaQueryWrapper<SysOrgInfo> queryWrapper = new LambdaQueryWrapper<SysOrgInfo>().orderByAsc(SysOrgInfo::getSort);
        return treeService.treeList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String id) {
        // 查询机构下是否存在子机构信息
        Long count = this.baseMapper.selectCount(new LambdaQueryWrapper<SysOrgInfo>().eq(SysOrgInfo::getParentId, id));
        if (count > 0) {
            throw new BizException("机构下存在子机构信息，不允许删除！");
        }
        // 删除机构信息
        sysOrgInfoMapper.deleteById(id);
        // 删除用户与机构的关联关系
        sysUserJoinOrgService.deleteByOrgId(id);
    }

}
